import numpy as np


class Finds:
    # 初始化保留输入的正例作为训练样本
    def __init__(self, samples, labels):
        positive = np.array([
            i for (i, label) in enumerate(labels)
            if label == 1
        ], dtype='int')
        self.samples = samples[positive, :]
        self.H = [None] * samples.shape[1]
        
        # print(self.samples)
        # print(self.H)
        # print()

    # 使用FINDS算法对训练样本进行训练
    def train(self):
        for sample in self.samples:
            i = 0
            for x in sample:
                if not self.H[i]:
                    self.H[i] = x
                elif x != self.H[i]:
                    self.H[i] = '?'
                i += 1

        print('rule of judging is:', self.H)

    # 根据已有规则进行预测
    def predict(self, input):
        i = 0
        for x in self.H:
            if input[i] == x or x == '?':
                i += 1
            else:
                break
        if i == len(self.H):
            print('True')
            return True
        else:
            print('False')
            return False
